import Vue from 'vue';
import App from './App.vue';
import router from './router';
import ElementUI from 'element-ui';
import VueI18n from 'vue-i18n';
import { messages } from './components/common/i18n';
import 'element-ui/lib/theme-chalk/index.css'; // 默认主题
// import './assets/css/theme-green/index.css'; // 浅绿色主题
import './assets/css/icon.css';
import '@/assets/icon/iconfont.css'
import './components/common/directives';
import 'babel-polyfill';
// 引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts

// 上传、下载、导出进度条
import 'nprogress/nprogress.css';
import Cookies from 'js-cookie';

import store from './store'
import * as utils from '@/utils/utils';
for (var key in utils) {
    Vue.prototype['$' + key] = utils[key];
}

import request from '@/utils/request'
Vue.prototype.$http = request;
// 后端多端口服务 每个都要拦截
for (let i = 0; i < request.length; i++) {
    // 默认post请求头为application/json
    request[i].defaults.headers.post['Content-Type'] = "application/json";

    request[i].interceptors.request.use(
        config => {
            if (Cookies.get('access_token')) {
                config.headers['Authorization'] = 'Bearer ' + Cookies.get('access_token')
                console.log('请求报文', config);
                localStorage.setItem('id', '2')
                localStorage.setItem('code', 'sms')
            } else if (Cookies.get('refresh_token')) {
                config.headers['Authorization'] = 'Bearer ' + Cookies.get('access_token')
                console.log('请求报文', config);
            } else {
                // 跳转登录
                router.replace({
                    path: '/login',
                }).catch(err => { })
            }

            return config;
        },
        error => {
            console.log(error);
            return Promise.reject(error);
        }
    );

    request[i].interceptors.response.use(
        response => {
            console.log('响应报文', response);
            // 3001表示token过期失效
            if (response.data.code == '3001') {
                Vue.prototype.$message.error({
                    message: response.data.message,
                    duration: 1500
                });
                Cookies.remove('access_token');
                Cookies.remove('refresh_token');
                router.replace({
                    path: '/login',
                }).catch(err => { })
            }
            return response;
        },
        error => {
            console.dir(error);
            return Promise.reject(error.response);
        }
    );
};

Vue.config.productionTip = false;
Vue.use(VueI18n);
Vue.use(ElementUI, {
    size: 'small'
});
const i18n = new VueI18n({
    locale: 'zh',
    messages
});

//使用钩子函数对路由进行权限跳转
router.beforeEach((to, from, next) => {
    console.log(to.meta.title, to.name, to.meta.requireAuth);
    document.title = to.meta.title || '万声音乐';
    if (to.meta.requireAuth) {
        if (!(Cookies.get('access_token') || Cookies.get('refresh_token'))) {
            return next({
                path: '/login'
            });
        }
    }
    // 已登录的情况再去登录页，跳转至首页
    if (to.name === 'Login') {
        if (Cookies.get('access_token') || Cookies.get('refresh_token')) {
            return next({
                path: '/'
            })
        }
    }
    next()
});

new Vue({
    router,
    store,
    i18n,
    render: h => h(App)
}).$mount('#app');
